#include <QHBoxLayout>
#include <QString>



#include "calculator.h"

calculator::calculator(QWidget *parent, Qt::WFlags flags)
	: QWidget(parent, flags)
{
//	ui.setupUi(this);

	number=0.0;
	ans=0.0;
	prevOperation="+";
	dotPushedFlag=0;
	dicemalCount=0;
	
	

//	QWidget *window = new QWidget(this);
	this->setFixedSize(480, 640);

	output = new QLineEdit(tr("0"));
	output->setAlignment(Qt::AlignRight);

	outputLayout = new QHBoxLayout();
	outputLayout->setAlignment(Qt::AlignHCenter);
	outputLayout->addWidget(output);


	

	QPushButton *oneButton = new QPushButton(tr("1"));
	QPushButton *twoButton = new QPushButton(tr("2"));
	QPushButton *threeButton = new QPushButton(tr("3"));
	QPushButton *fourButton = new QPushButton(tr("4"));
	QPushButton *fiveButton = new QPushButton(tr("5"));
	QPushButton *sixButton = new QPushButton(tr("6"));
	QPushButton *sevenButton = new QPushButton(tr("7"));
	QPushButton *eightButton = new QPushButton(tr("8"));
	QPushButton *nineButton = new QPushButton(tr("9"));
	QPushButton *dotButton = new QPushButton(tr("."));
	QPushButton *zeroButton = new QPushButton(tr("0"));
	QPushButton *equalButton = new QPushButton(tr("="));

	connect(oneButton, SIGNAL(clicked()), this, SLOT(onePushed()));
	connect(twoButton, SIGNAL(clicked()), this, SLOT(twoPushed()));
	connect(threeButton, SIGNAL(clicked()), this, SLOT(threePushed()));
	connect(fourButton, SIGNAL(clicked()), this, SLOT(fourPushed()));
	connect(fiveButton, SIGNAL(clicked()), this, SLOT(fivePushed()));
	connect(sixButton, SIGNAL(clicked()), this, SLOT(sixPushed()));
	connect(sevenButton, SIGNAL(clicked()), this, SLOT(sevenPushed()));
	connect(eightButton, SIGNAL(clicked()), this, SLOT(eightPushed()));
	connect(nineButton, SIGNAL(clicked()), this, SLOT(ninePushed()));
	connect(zeroButton, SIGNAL(clicked()), this, SLOT(zeroPushed()));
	connect(dotButton, SIGNAL(clicked()), this, SLOT(dotPushed()));
	connect(equalButton, SIGNAL(clicked()), this, SLOT(getValue()));

	numberLayout = new QGridLayout();
	numberLayout->addWidget(oneButton,0,0);
	numberLayout->addWidget(twoButton,0,1);
	numberLayout->addWidget(threeButton,0,2);
	numberLayout->addWidget(fourButton,1,0);
	numberLayout->addWidget(fiveButton,1,1);
	numberLayout->addWidget(sixButton,1,2);
	numberLayout->addWidget(sevenButton,2,0);
	numberLayout->addWidget(eightButton,2,1);
	numberLayout->addWidget(nineButton,2,2);
	numberLayout->addWidget(dotButton,3,0);
	numberLayout->addWidget(zeroButton,3,1);
	numberLayout->addWidget(equalButton,3,2);

	plusButton = new QPushButton(tr("+"));
	minusButton = new QPushButton(tr("-"));
	multiplyButton = new QPushButton(tr("*"));
	divideButton = new QPushButton(tr("/"));

	connect(plusButton, SIGNAL(clicked()), this, SLOT(add()));
	connect(minusButton, SIGNAL(clicked()), this, SLOT(minus()));
	connect(multiplyButton, SIGNAL(clicked()), this, SLOT(multiply()));
	connect(divideButton, SIGNAL(clicked()), this, SLOT(divide()));

	operationLayout = new QVBoxLayout();
	operationLayout->addWidget(plusButton);
	operationLayout->addWidget(minusButton);
	operationLayout->addWidget(multiplyButton);
	operationLayout->addWidget(divideButton);

	editLayout = new QHBoxLayout();
	QPushButton *clearNumberButton = new QPushButton(tr("C"));
	QPushButton *clearAllButton = new QPushButton(tr("CE"));
	connect(clearNumberButton, SIGNAL(clicked()), this, SLOT(clearNumber()));
	connect(clearAllButton, SIGNAL(clicked()), this, SLOT(clearAll()));
	editLayout->addWidget(clearNumberButton);
	editLayout->addWidget(clearAllButton);

	calLayout = new QHBoxLayout();	
	calLayout->addLayout(numberLayout);
	calLayout->addLayout(operationLayout);

	layout = new QVBoxLayout(this);
	layout->addLayout(outputLayout);
//	layout->addWidget(output);
	layout->addLayout(calLayout);
	layout->addLayout(editLayout);
	layout->setSizeConstraint(QLayout::SetFixedSize);

	
	setLayout(layout);

	setWindowTitle(tr("Calculator"));
}

calculator::~calculator()
{
	this->getValue();

}

void calculator::add()
{
	dotPushedFlag=0;
	this->getValue();
	
	prevOperation="+";
	
}

void calculator::minus()
{
	dotPushedFlag=0;
	this->getValue();
	prevOperation="-";

}

void calculator::multiply()
{
	dotPushedFlag=0;
	this->getValue();
	prevOperation="*";

}

void calculator::divide()
{
	dotPushedFlag=0;
	this->getValue();
	prevOperation="/";

}

void calculator::getValue()
{
	dotPushedFlag=0;

	if(prevOperation == "+")
		ans=ans+number;
	if(prevOperation == "-")
		ans=ans-number;
	if(prevOperation == "*")
		ans=ans*number;
	if(prevOperation == "/")
	{
		if(number==0)
			ans=0;
		else
			ans=ans/number;
	}

	this->displayValue(ans);

	number=0;
	prevOperation="+";

}

void calculator::onePushed()
{
	if(dotPushedFlag==1)
	{
		dicemalCount+=1;
		number=number+1.0/(10^dicemalCount);
	}
	else
		number=number*10+1;
	
	this->displayValue(number);
}

void calculator::twoPushed()
{
	if(dotPushedFlag==1)
	{
		dicemalCount+=1;
		number=number+2/(dicemalCount*10);
	}
	else
		number=number*10+2;
	this->displayValue(number);

}

void calculator::threePushed()
{
	if(dotPushedFlag==1)
	{
		dicemalCount+=1;
		number=number+3/(dicemalCount*10);
	}
	else
		number=number*10+3;
	this->displayValue(number);

}

void calculator::fourPushed()
{
	if(dotPushedFlag==1)
	{
		dicemalCount+=1;
		number=number+4/(dicemalCount*10);
	}
	else
		number=number*10+4;
	this->displayValue(number);

}

void calculator::fivePushed()
{
	if(dotPushedFlag==1)
	{
		dicemalCount+=1;
		number=number+5/(dicemalCount*10);
	}
	else
		number=number*10+5;
	this->displayValue(number);

}

void calculator::sixPushed()
{
	if(dotPushedFlag==1)
	{
		dicemalCount+=1;
		number=number+6/(dicemalCount*10);
	}
	else
		number=number*10+6;
	this->displayValue(number);

}

void calculator::sevenPushed()
{
	if(dotPushedFlag==1)
	{
		dicemalCount+=1;
		number=number+7/(dicemalCount*10);
	}
	else
		number=number*10+7;
	this->displayValue(number);

}

void calculator::eightPushed()
{
	if(dotPushedFlag==1)
	{
		dicemalCount+=1;
		number=number+8/(dicemalCount*10);
	}
	else
		number=number*10+8;
	this->displayValue(number);

}

void calculator::ninePushed()
{
	if(dotPushedFlag==1)
	{
		dicemalCount+=1;
		number=number+9/(dicemalCount*10);
	}
	else
	number=number*10+9;
	this->displayValue(number);

}

void calculator::zeroPushed()
{
	if(dotPushedFlag==1)
	{
		dicemalCount+=1;
		
	}
	else
	number=number*10+0;
	this->displayValue(number);

}

void calculator::dotPushed()
{
	dotPushedFlag=1;

}

void calculator::displayValue(float value)
{
	
	output->setText(QString::number(value,'g', 10));
	output->update();
}

void calculator::clearNumber()
{
	number=0;
	dotPushedFlag=0;
	this->displayValue(number);
}

void calculator::clearAll()
{
	number=0;
	dotPushedFlag=0;
	ans=0;
	this->displayValue(ans);
}
